from rest_framework.views import APIView
from apps.UserControl.models.user import User
from apps.HttpTestcase.models import *
from utils.response import SuccessResponse
from apps.HttpTestcase.models.testcase_reports import TestcaseReports
from django.db.models import Avg,Max,Min,Count,Sum
from django.db.models import F, Q
from django.utils.timezone import now, timedelta


class HomeApiView(APIView):
    def post(self, request, *args, **kwargs):
        # 统计项目总数
        project_all = Projects.objects.filter(is_delete=False).count()
        api_all = Interfaces.objects.filter(is_delete=False).count()
        method_all = Ext_MethodModel.objects.filter(is_delete=False).count()
        testcase_all = Testcases.objects.filter(is_delete=False).count()
        testsuite_all = Testsuite.objects.filter(is_delete=False).count()
        user_all = User.objects.filter(is_active=True).count()
        reports_pass = TestcaseReports.objects.filter(Q(is_delete=False, status='PASS')).count()
        reports_fail = TestcaseReports.objects.filter(Q(is_delete=False, status='FAIL')).count()
        # cur_time = datetime.datetime.now()

        data = []
        for i in range(0, 7):
            data_time = now().date() + timedelta(days=-i)
            # __startswith以什么开头
            pass_count = TestcaseReports.objects.filter(Q(create_time__startswith=data_time,is_delete=False, status='PASS')).count()
            fail_count = TestcaseReports.objects.filter(Q(create_time__startswith=data_time, is_delete=False, status='FAIL')).count()
            # pass_count = TestcaseReports.objects.filter(Q(create_time__year=data_time.year, create_time__month=data_time.month, create_time__day=data_time.day, is_delete=False, status='PASS')).count()
            # fail_count = TestcaseReports.objects.filter(Q(create_time__year=data_time.year, create_time__month=data_time.month, create_time__day=data_time.day, is_delete=False, status='FAIL')).count()
            dict = {
              "data_time": data_time,
              "pass_count": pass_count,
              "fail_count": fail_count
            }
            data.append(dict)
        # week_all = TestcaseReports.objects.filter(Q(create_time__gte=week, is_delete=False)).extra(select={"create_time": "DATE_FORMAT(create_time, '%%Y-%%m-%%d')"})
        # week_count = week_all.values('status', 'create_time').annotate(Count('create_time')).order_by('create_time')
        # list = []
        # for item in week_count:
        #     list.append(item)

        # week_fail = TestcaseReports.objects.filter(Q(create_time__gte=week, is_delete=))
        res = {
            "project_all": project_all,
            "api_all": api_all,
            "method_all": method_all,
            "testcase_all": testcase_all,
            "testsuite_all": testsuite_all,
            "user_all": user_all,
            "reports_pass": reports_pass,
            "reports_fail": reports_fail,
            "week_count": data,
        }
        return SuccessResponse(data=res, msg='统计成功')
